﻿@using Gurux.DLMS.AMI.Shared.DIs
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.Rest
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Client.Helpers
@using Gurux.DLMS.AMI.Module
@using Gurux.DLMS.AMI.Client.Pages.Device

<div class="row">
    <div style="width:100%">
        <div class="form-group">
            <label>@Properties.Resources.Protocol</label>
            <select class="form-select" @bind="Protocol">
                @foreach (var it in NetworkTypeList)
                {
                    <option value="@it">@it</option>
                }
            </select>
        </div>
        @if (!IsServer)
        {
            <div class="form-group">
                <label>@Properties.Resources.HostName</label>
                <InputText id="name" class="form-control"
                       @bind-Value="HostName" />
                <ValidationMessage For="@(() => HostName)" />
            </div>
        }
        <div class="form-group">
            <label>@Properties.Resources.Port</label>
            <InputNumber id="port" class="form-control"
                         @bind-Value="Port" />
            <ValidationMessage For="@(() => Port)" />
        </div>
    </div>
</div>
@code {
    Gurux.Net.NetworkType[] NetworkTypeList = new Gurux.Net.NetworkType[] { Gurux.Net.NetworkType.Tcp, Gurux.Net.NetworkType.Udp };

    private Gurux.Net.GXNet _net = new Gurux.Net.GXNet();

    private string? _settings;

    /// <summary>
    /// Host name is hidden if only server settings are shown.
    /// </summary>
    [Parameter]
    public bool IsServer { get; set; }

    [Parameter]
    public string? Settings
    {
        get => _settings;
        set
        {
            if (_settings != value)
            {
                _settings = value;
                SettingsChanged.InvokeAsync(value);
            }
        }
    }

    /// <summary>
    /// Notified when settings are updated.
    /// </summary>
    [Parameter]
    public EventCallback<string?> SettingsChanged { get; set; }

    public string? HostName
    {
        get
        {
            return _net?.HostName;
        }
        set
        {
            _net.HostName = value;
            Settings = _net.Settings;
        }
    }

    public int Port
    {
        get
        {
            return _net.Port;
        }
        set
        {
            _net.Port = value;
            Settings = _net.Settings;
        }
    }

    public Gurux.Net.NetworkType Protocol
    {
        get
        {
            return _net.Protocol;
        }
        set
        {
            _net.Protocol = value;
            Settings = _net.Settings;
        }
    }

    /// <summary>
    /// Initialise network settings.
    /// </summary>
    protected override void OnInitialized()
    {
        try
        {
            _net.Settings = _settings;
        }
        catch (Exception)
        {
            //Settings might fail when media type is changed.
        }
    }

    /// <summary>
    /// Validate network settings.
    /// </summary>
    public void Validate(GXValidator sender)
    {
        if (!IsServer && string.IsNullOrEmpty(HostName))
        {
            sender.AddError(() => HostName, "Invalid host name.");
        }
        if (Port == 0)
        {
            sender.AddError(() => Port, "Invalid port.");
        }
    }
}
